package com.macro.mall.portal.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.macro.mall.portal.config.AlipayConfig;
import com.macro.mall.portal.service.SyntonyService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Slf4j
public class SyntonyServiceImpl implements SyntonyService {
    @Autowired
    private AlipayConfig alipayConfig;
    //支付宝回调接口
    @Override
    public String pay(HttpServletRequest request, Map<String, String> allParams) {
        boolean signVerified = false;
        try {
            signVerified = AlipaySignature.rsaCheckV1(allParams, alipayConfig.getAlipayPublicKey(), "UTF-8", "RSA2");
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        if (signVerified) {
            // 验证成功，处理业务逻辑
            // TODO: 根据trade_status判断交易状态，并处理业务
            String tradeStatus = allParams.get("trade_status");
            if ("TRADE_SUCCESS".equals(tradeStatus)) {
                // 交易成功，处理订单支付成功的业务逻辑
            }
            return "success"; // 返回支付宝期望的成功标识
        } else {
            return "failure"; // 验证失败
        }
    }
}
